<?php
namespace app\index\controller;
use app\index\service\MarketService;
use think\Request;
use think\Db;
use think\Session;


class Market extends Base
{

		public  function _initialize(){
				$this->heade();
				$this->get_session();
	 	}

    public function index()
    {

        return $this->fetch();
    }

    /**
     * pid页面
    */
	/*  public function pid()
    {
        if(Request::instance()->isGet()){
            $marketService = new MarketService;
            $data = $marketService->pidGet();
            if($data['code'] && $data['code'] == -1){
                $this->redirect('index/Login/login');
            }
            // dump($data['result']['pids']);die;
            $this->assign([
                'data'      => $data['result'],
                'times'     => $data['times'],
            ]);
            return $this->fetch();
        }
    }*/

    //pid 页面
   public function pid(){
		    $user_session = Session::get('date');
			$arr = Db::table('think_user')->where('id','EQ',$user_session['id'])->field('session_key,up_session_time')->find();
			$arr['time']= strtotime('-7 day');
			$new_time = $arr['up_session_time']-$arr['time'];
			if($arr['up_session_time']){
			if($new_time>0){
				$num= floor($new_time/(60*60*24)).'天';
			}else{
				$num='以过期请重新授权';
			}
			}else{
				$num='请授权';
			}
			//$pid_arr = Db::table('think_pid')->where('uid','EQ',$user_session['id'])->field('pid,status')->select();
			$pid = Db::table('think_pid')->where('uid','EQ',$user_session['id'])->where('status','EQ',1)->value('pid');
			$this->assign('pid',$pid);
			$this->assign('num',$num);
			$this->assign('arr',$arr);
			return $this->fetch();
    }



    /**
     * 新增pid
    */
    /*public function insertPid()
    {
        $marketService = new MarketService;
        if(Request::instance()->isGet()){
            $data = $marketService->insertPidGet();
            if($data['code'] == -1){
                $this->redirect('index/Login/login');
            }
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            return json($marketService->insertPidPost($post));
        }
    }*/

    /**
     * 编辑pid
    */
    /*public function editPid()
    {
        $marketService = new MarketService;
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            $data = $marketService->editPidGet($id);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            return json($marketService->editPidPost($post['field']));
        }
    }*/

    //添加PID页面
    public function insertPid(){
       return $this->fetch();
    }

	//添加PID方法
	 public function insertPidNew(){
	     $user_session = Session::get('date');
		 $pid = input('pid');
		 if($pid==''){
		 	$data['code'] = 0;
		    $data['msg'] = '请输入PID';
		 }else{
			 $pid_num = Db::table('think_pid')->where('pid','EQ',$pid)->find();
			 if(!empty($pid_num)){
			 	$data['code'] = 0;
			    $data['msg'] = '该PID以绑定';
			 }else{
			     $pid_arr = Db::table('think_pid')->where('uid','EQ',$user_session['id'])->find();
				 if(empty($pid_arr)){
				 	   $data=['uid'=>$user_session['id'],'pid'=>$pid];
					    $res_new = Db::table('think_pid')->insert($data);
						if($res_new){
							 $data['code'] = 1;
						     $data['msg'] = '添加成功';
						}else{
							$data['code'] = 0;
						    $data['msg'] = '系统繁忙,请稍后再试';
						}
				 }else{
					 $res = Db::table('think_pid')->where('uid','EQ',$user_session['id'])->update(['status'=>2]);
					 if($res){
					 	$data=['uid'=>$user_session['id'],'pid'=>$pid];
					    $res = Db::table('think_pid')->insert($data);
					    if($res){
					 	    $data['code'] = 1;
						    $data['msg'] = '添加成功';
					     }else{
					 	   $data['code'] = 0;
						   $data['msg'] = '系统繁忙,请稍后再试';
					    }
					 }else{
					 	  $data['code'] = 0;
						  $data['msg'] = '系统繁忙,请稍后再试';
					 }
				 }
				 }
		 }
            return $data;
    }


    //更新PID的渲染页面
    public function editPid(){
     $user_session = Session::get('date');
	 $pid_arr = Db::table('think_pid')->where('uid','EQ',$user_session['id'])->field('pid,status')->select();
	 $this->assign('pid_uid',$user_session['id']);
	 $this->assign('pid_arr',$pid_arr);
	 return $this->fetch();
    }

	//更新PID
	public function update_pid(){
	 $data = input();
	 $pid = $data['pid'];
	 $id =$data['id'];
	 $res = Db::table('think_pid')->where('uid','EQ',$id)->update(['status'=>2]);
	 if($res){
	    $arr = Db::table('think_pid')->where('uid','EQ',$id)->where('pid','EQ',$pid)->update(['status'=>1]);
		 if($arr){
		 	$data['code'] = 1;
		    $data['msg'] = '更新成功';
		 }else{
		 	$data['code'] = 0;
		    $data['msg'] = '系统繁忙,请稍后再试';
		 }
	 }else{
	 	$data['code'] = 0;
		$data['msg'] = '系统繁忙,请稍后再试';
	 }
	 return $data;

	}


   //绑定session值
    public function sureSession(){
     $user_session = Session::get('date');
	 $session_key = input('session_key');
	 $data['session_key']=$session_key;
	 $data['up_session_time']=time();
	 $res = Db::table('think_user')->where('id','EQ',$user_session['id'])->update($data);
	 if($res){
	 	$date['code']=1;
		$date['msg']='绑定成功';
	 }else{
	 	$date['code']=0;
		$date['msg']='系统繁忙,请稍后再试';
	 }
	 return $date;
    }

    /**
     * 重新授权session
    */
   /* public function sureSession()
    {
        if(Request::instance()->isPost()){
            $session_key = Request::instance()->post('session_key');
            $marketService = new MarketService;
            return json($marketService->sureSession($session_key));
        }
    }*/





    /**
     * 个人信息
    */
    public function infomation()
    {
        $marketService = new MarketService;
        if(Request::instance()->isGet()){
            $data = $marketService->infomationGet();
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            return json($marketService->infomationPost($post));
        }
    }

    /**
     * 账号安全
    */
    public function accountSafety()
    {
        if(Request::instance()->isGet()){
            echo 444;die;
            return $this->fetch();
        }else{

        }
    }

    //推广人消息中心
    public function news()
    {
    	$arr= Session::get('date');
		$id =$arr['id'];
		$where = [-1,0,$id];
	   	$messages = Db::name('system_message')->where('uid','in',implode(',',$where))->where('type','EQ',3)->paginate(10,false,['query'=>input()]);
		//dump($messages);die;
		$this->assign('messages',$messages);
	    return $this->fetch();
    }

		/**
		 * 推广人结算
		*/
		public function balance()
    {
				$marketService = new MarketService;
				if(Request::instance()->isGet()){
						$param = Request::instance()->get();
						$data = $marketService->balance($param);
						$arr_new= Session::get('date');
						$uid = $arr_new['id'];
						$re = Db::table('think_bankcard')->where('uid','EQ',$uid)->find();
						$this->assign('re',$re);
						$this->assign('data',$data);
						return $this->fetch();
				}else{
						$post = Request::instance()->post();
						return json($marketService->balancePost($post));
				}
    }
	//推广人修改银行卡账号的页面展示
	public function updataBindCard(){
		$arr_new= Session::get('date');
	    $uid = $arr_new['id'];
		$re = Db::table('think_bankcard')->where('uid','EQ',$uid)->find();
		$this->assign('re',$re);
		return $this->fetch();
	}
	  //推广人修改银行卡号的功能
	  public  function updateBindcard_new(){
		 $arr = Request::instance()->post();
		 $iscredit = preg_match('/^(\d{16}|\d{19}|\d{17})$/', $arr['credit'], $matches); //验证银行卡账号
		 $isnumbers = preg_match('/^[1-9][0-9]{4,14}$/', $arr['numbers'], $matches); //验证手机号
			if($iscredit){
				if($isnumbers){
					$id = $arr['id'];
					$arr['add_time']=time();
					$re = Db::table('think_bankcard')->where('id','EQ',$id)->update($arr);
					if($re){
						$data['code'] = 2;
				        $data['msg']='修改成功';
					}else{
						$data['code'] = 1;
				        $data['msg']='系统忙请稍后再试';
					}
			    }else{
			    	$data['code'] = 1;
			        $data['msg']='请输入正确的手机号';
			    }
			}else{
				$data['code'] = 1;
		        $data['msg']='请输入正确的行卡账号';
			}	
		return $data;
		
	  }
	

    //详情页结算详情
    public function balance_xq(){
      	$marketService = new MarketService;
				if(Request::instance()->isGet()){
						$param = Request::instance()->param();
						// dump($param);die;
						$data = $marketService->balance_xq($param);
						$this->assign('data',$data);
						return $this->fetch();
				}else{
						$post = Request::instance()->post();
						return json($marketService->balance_xq_post($post));
				}
    }

		//推广人绑定银行卡
		public function bindCard(){
			 	return $this->fetch();
		}
		
			//推广人绑定银行卡 出来图片
	public function bindImg(){
//		dump($_FILES);die;
		$file = request()->file('file');
				// 移动到框架应用根目录/public/uploads/ 目录下
				if($file){
						$info = $file->move(ROOT_PATH . 'public/static' . DS . 'bankcar_img');
						if($info){
								return json(['code'=>1,'msg'=>'上传成功','data'=>array('src'=>'/static/bankcar_img/'.$info->getSaveName(),'name'=>$info->getSaveName())]);
						}else{
								return json(['code'=>-1,'msg'=>$file->getError()]);
						}
				}
	}	
		
      //推广人绑定银行卡的数据存入数据库
      
      public function addBindcard(){
      	$arr = Request::instance()->post();
		$certificate = $arr['certificate'];
		$credit = $arr['credit'];
		$numbers = $arr['numbers'];
		$iscertificate = preg_match('/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/', $certificate, $matches); //验证身份证号
		$iscredit = preg_match('/^(\d{16}|\d{19}|\d{17})$/', $credit, $matches); //验证银行卡账号
		$isnumbers = preg_match('/^[1-9][0-9]{4,14}$/', $numbers, $matches); //验证手机号
		if($iscertificate){
			if($iscredit){
				if($isnumbers){
					$date= Session::get('date');
					$arr['uid'] = $date['id'];
					$arr['add_time']=time();
					$re = Db::table('think_bankcard')->insert($arr);
					if($re){
						$data['code'] = 2;
				        $data['msg']='提交成功';
					}else{
						$data['code'] = 1;
				        $data['msg']='系统忙请稍后再试';
					}
			    }else{
			    	$data['code'] = 1;
			        $data['msg']='请输入正确的手机号';
			    }
			}else{
				$data['code'] = 1;
		        $data['msg']='请输入正确的行卡账号';
			}	
		}else{
			$data['code'] = 1;
	        $data['msg']='请输入正确的身份证号';
		}
		return $data;
      }

		//淘宝session取值
		public function iframe_page(){
				return $this->fetch();
		}

		/**
		 * 提现
		*/
		public function cash()
		{
				$marketService = new MarketService;
				if(Request::instance()->isGet()){
						//查看是否绑定银行卡
						$data = $marketService->hasBindCard(2);
						if(!$data){
								$this->redirect('bindCard');
						}
						//用户信息
						$userInfomation = $marketService->userInfomation();
						// dump($userInfomation);die;
						$this->assign([
								'data'           => $data,
								'userInfomation' => $userInfomation
						]);
						return $this->fetch();
				}else{
						$account = Request::instance()->post('account');
						return json($marketService->cashPost($account));
				}
		}

   //pid绑定说明
   public function pidillustrate(){
   	return $this->fetch();
   }
   
}
